package com.taxshare.purchaser.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.minio.utils.MinIOUtils;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.purchaser.entity.ElectronicledgerEntity;
import com.taxshare.purchaser.entity.InvoiceDetailEntity;
import com.taxshare.purchaser.entity.InvoicePoolEntity;
import com.taxshare.purchaser.mapper.ElectronicledgerMapper;
import com.taxshare.purchaser.mapper.InvoicePoolMapper;
import com.taxshare.purchaser.view.dto.InvoiceAuthFlagDTO;
import com.taxshare.purchaser.view.dto.InvoiceAuthFlagMainDTO;
import com.taxshare.purchaser.view.dto.InvoicePoolDTO;
import com.taxshare.response.beans.Response;
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YANG
 * @date 2021/4/12
 * @Description:发票池
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class InvoicePoolService extends ServiceImpl<InvoicePoolMapper, InvoicePoolEntity> {

    private static String timeStart = " 00:00:00";
    private static String timeEnd = " 23:59:59";

    @Autowired
    InvoiceDetailService invoiceDetailService;

    @Autowired
    MinIOUtils minIOUtils;

    @Autowired
    private MinioClient instance;

    @Autowired
    private CommService commService;

    @Autowired
    ElectronicledgerMapper electronicledgerMapper;


    /**
     * 发票池查询【发票池】
     *
     * @param query
     * @return
     */
    public Response invoicePoolQuery(PageRequest<InvoicePoolDTO> query) {
        QueryWrapper<InvoicePoolEntity> invoicePoolQuery = new QueryWrapper<>();
        InvoicePoolDTO invoicePoolDTO = query.getInfo();
        if (StrUtil.isNotBlank(invoicePoolDTO.getInvoiceCode())) {
            invoicePoolQuery.eq("invoice_code", invoicePoolDTO.getInvoiceCode());
        }
        if (StrUtil.isNotBlank(invoicePoolDTO.getInvoiceNo())) {
            invoicePoolQuery.eq("invoice_no", invoicePoolDTO.getInvoiceNo());
        }
        if (StrUtil.isNotBlank(invoicePoolDTO.getInvoiceType())) {
            invoicePoolQuery.eq("invoice_type", invoicePoolDTO.getInvoiceType());
        }
        if (StrUtil.isNotBlank(invoicePoolDTO.getInvoiceState())) {
            invoicePoolQuery.eq("invoice_state", invoicePoolDTO.getInvoiceState());
        }
        if (StrUtil.isNotBlank(invoicePoolDTO.getCheckStatus())) {
            invoicePoolQuery.eq("check_status", invoicePoolDTO.getCheckStatus());
        }
        if (StrUtil.isNotBlank(invoicePoolDTO.getRulesStatus())) {
            invoicePoolQuery.eq("rules_status", invoicePoolDTO.getRulesStatus());
        }
        if (StrUtil.isNotBlank(invoicePoolDTO.getInvoiceDateStart()) && StrUtil.isNotBlank(invoicePoolDTO.getInvoiceDateEnd())) {
            invoicePoolQuery.between("invoice_date", invoicePoolDTO.getInvoiceDateStart() + timeStart, invoicePoolDTO.getInvoiceDateEnd() + timeEnd);
        }
        if (StrUtil.isNotBlank(invoicePoolDTO.getCreateTimeStrStart()) && StrUtil.isNotBlank(invoicePoolDTO.getCreateTimeStrEnd())) {
            invoicePoolQuery.between("create_time_str", invoicePoolDTO.getCreateTimeStrStart() + timeStart, invoicePoolDTO.getCreateTimeStrEnd() + timeEnd);
        }
        invoicePoolQuery.orderByDesc("create_time");
        //权限处理
        List<String> taxNos = commService.getTaxNumbers();
        invoicePoolQuery.in("purchaser_tax_no",taxNos);
        IPage<InvoicePoolEntity> invoicePoolEntityIPage = PageHelp.siamplePage(this, invoicePoolQuery, query.getPage(), InvoicePoolEntity.class);
        return Response.success(invoicePoolEntityIPage);
    }

    /**
     * 发票明细查看【发票池】
     *
     * @param query
     * @return
     */
    public Response invoiceDetailQuery(PageRequest<InvoicePoolDTO> query) {
        QueryWrapper<InvoiceDetailEntity> detailQuery = new QueryWrapper<>();
        InvoicePoolDTO invoicePoolDTO = query.getInfo();
        detailQuery.eq("link_id", invoicePoolDTO.getUuid());
        IPage<InvoiceDetailEntity> invoiceDetailEntityIPage = PageHelp.siamplePage(invoiceDetailService, detailQuery, query.getPage(), InvoiceDetailEntity.class);
        return Response.success(invoiceDetailEntityIPage);
    }

    /**
     * 获取bpm发票认证标记
     *
     * @param beans
     * @return
     */
    public Response authApplyGetAuthFlag(InvoiceAuthFlagMainDTO beans) {
        List<InvoiceAuthFlagDTO> authFlagDTOS = beans.getData();
        if (CollUtil.isNotEmpty(authFlagDTOS)) {
            for (InvoiceAuthFlagDTO authFlagDTO : authFlagDTOS) {
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("invoice_code", authFlagDTO.getInvoiceCode());
                queryWrapper.eq("invoice_no", authFlagDTO.getInvoiceNo());
                InvoicePoolEntity invoicePoolEntity = new InvoicePoolEntity();
                invoicePoolEntity.setInvoiceFlag(authFlagDTO.getInvoiceFlag());
                invoicePoolEntity.setDocNo(authFlagDTO.getDocNo());
                invoicePoolEntity.setDocState(authFlagDTO.getDocState());
                invoicePoolEntity.setAccountItem(authFlagDTO.getAccountItem());
                baseMapper.update(invoicePoolEntity, queryWrapper);
                ElectronicledgerEntity electronicledgerEntity = new ElectronicledgerEntity();
                electronicledgerEntity.setInvoiceFlag(authFlagDTO.getInvoiceFlag());
                electronicledgerEntity.setDocNo(authFlagDTO.getDocNo());
                electronicledgerEntity.setDocState(authFlagDTO.getDocState());
                electronicledgerEntity.setAccountItem(authFlagDTO.getAccountItem());
                electronicledgerMapper.update(electronicledgerEntity, queryWrapper);
            }
        } else {
            return Response.error("PUR_ERROR_006");
        }
        return Response.success();
    }

}
